package com.csg.ses.service.ses.impl;

import com.csg.ses.common.utils.CommonUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import lombok.RequiredArgsConstructor;
import com.csg.ses.common.utils.MybatisPlusUtil;
import org.springframework.transaction.annotation.Transactional;
import com.csg.ses.vo.common.IdListVo;

import com.csg.ses.dao.ses.NewsInfoDao;
import com.csg.ses.entity.ses.NewsInfoEntity;
import com.csg.ses.service.ses.NewsInfoService;

/**
 * 
 * @author zhanggongrong
 * @date 2021-04-05 14:18:22
 */
@Service
@RequiredArgsConstructor
public class NewsInfoServiceImpl extends ServiceImpl<NewsInfoDao, NewsInfoEntity> implements NewsInfoService {
    private final NewsInfoDao newsInfoDao;

    @Override
    public Result retrieve(NewsInfoEntity newsInfo, Long current, Long size) {
        try {
            Page<NewsInfoEntity> page = MybatisPlusUtil.initPage(current, size, true);
            QueryWrapper<NewsInfoEntity> qw = new QueryWrapper();
            return new Result(newsInfoDao.retrieve(page, qw), ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Result queryAllNews() {
        return new Result(newsInfoDao.getAllNews());
    }

    @Override
    @Transactional
    public Result del(IdListVo idList) {
        try {
            if (idList != null && idList.getIdList().size() > 0) {
                int num = newsInfoDao.deleteBatchIds(idList.getIdList());
                return new Result(num + "条记录删除成功！", ResultStatus.SUCCESS);
            } else {
                return new Result("未指定需要删除的记录！", ResultStatus.FAILED);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Result getById(String id) {
        if (!CommonUtil.isEmpty(id)) {
            return new Result(newsInfoDao.selectById(id), ResultStatus.SUCCESS);
        } else {
            throw new RuntimeException("id不能为空！");
        }
    }

    @Override
    @Transactional
    public boolean save(NewsInfoEntity newsInfo) {
        try {
            if (!CommonUtil.isEmpty(newsInfo.getNewsId()+"")) {
                newsInfoDao.updateById(newsInfo);
            } else {
                newsInfo.setNewsId(newsInfoDao.getNextId().toString());
                newsInfoDao.insert(newsInfo);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}